/********************************************
 * 功能说明: 
 * 模块名称: 
 * 系统名称: 
 * 软件版权: 北京银杉金服科技有限公司
 * 系统版本: 1.0.0
 * 开发人员: zhangfb
 * 开发时间: 2019/1/13 20:21
 * 审核人员: 
 * 相关文档: 
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.hyacinth.server.bean;

import com.hyacinth.server.config.AppConfig;
import com.hyacinth.server.util.ClassScanner;
import com.sun.xml.internal.bind.v2.TODO;

import java.util.Map;

/**
 * Bean管理器
 * @author zhangfb
 * @version 1.0.0.1
 * @since JDK 1.8
 */
public final class AuroraBeanManager {

    private AuroraBeanManager(){}

    private static volatile AuroraBeanManager auroraBeanManager;

    public static AuroraBeanManager getInstance() {
        if (auroraBeanManager == null) {
            synchronized (AuroraBeanManager.class) {
                if (auroraBeanManager == null) {
                    auroraBeanManager = new AuroraBeanManager();
                }
            }
        }
        return auroraBeanManager;
    }

    private static AuroraBeanFactory auroraBeanFactory;

    /**
     * get bean
     */
    public Object getBean(String name) throws Exception {
        return auroraBeanFactory.getBean(name) ;
    }

    /**
     * release all beans
     */
    public void releaseBean(){
        auroraBeanFactory.releaseBean();
    }

    /**
     * 初始化bean
     */
    public void init() throws Exception {
        String rootPackageName = AppConfig.getInstance().getRootPackageName();
        // 获取bean工厂
        auroraBeanFactory = ClassScanner.getAuroraBeanFactory(rootPackageName);
        // 获取action
        Map<String, Class<?>> auroraAction = ClassScanner.getAuroraAction(rootPackageName);
        // 注册
        for (Map.Entry<String, Class<?>> entry : auroraAction.entrySet()) {
            auroraBeanFactory.register(entry.getKey(), entry.getValue());
        }
    }
}
